﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class StaffEntry
    {
        public void ImportList(Classes.ArrayOfStaffEnrty staffEntryCollection)
        {
           
            var mdb = new MedregDBDataContext();

            var sLpu = new Classes.LPU();
            var sPost = new Classes.Post();
            var sSub = new Classes.Subdivision();
            var sSte = new Classes.StaffEntry();

            var lpu = new LPU();
            lpu.ImportList(sLpu.getLPUList(staffEntryCollection));

            var post = new Post();
            post.ImportList(sPost.getPostList(staffEntryCollection));

            var sub = new Subdivision();
            sub.ImportList(sSub.getSubdivisionList(staffEntryCollection));

           
            foreach (var staffentry in sSte.getStaffEntryList(staffEntryCollection))
            {
                StaffEntry staffentry1 = new StaffEntry()
                {
                    UZ = staffentry.UZ.ID,
                    Branch = staffentry.Branch.ID,
                    StuffPost = staffentry.StuffPost.ID,
                    Quantity = staffentry.Quantity,
                    Comment = staffentry.Comment
                };

               if (!mdb.StaffEntry.Contains(staffentry1))
               {
                   mdb.StaffEntry.InsertOnSubmit(staffentry1);
               }
                
                
               StaffEntry query;

                
               if (mdb.StaffEntry.LongCount() == 0)
                {
                    mdb.StaffEntry.InsertOnSubmit(staffentry1);
                }
                else
                {
                    try
                    {
                        query = (from ste in mdb.StaffEntry
                                     where
                                         new {ste.UZ, ste.Branch, ste.StuffPost}.Equals(
                                             new {staffentry1.UZ, staffentry1.Branch, staffentry1.StuffPost})
                                     select ste).Single();

                        
                        query.UZ = staffentry1.UZ;
                        query.Branch = staffentry1.Branch;
                        query.StuffPost = staffentry1.StuffPost;
                        query. Quantity = staffentry1.Quantity;
                        query.Comment = staffentry1.Comment;
                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        mdb.StaffEntry.InsertOnSubmit(staffentry1);
                    }
                }



                try
                {
                    mdb.SubmitChanges(ConflictMode.FailOnFirstConflict);
                }
                catch (ChangeConflictException e)
                {
                    Console.WriteLine("Optimistic concurrency error.");
                    Console.WriteLine(e.Message);
                    foreach (ObjectChangeConflict occ in mdb.ChangeConflicts)
                    {
                        MetaTable metatable = mdb.Mapping.GetTable(occ.Object.GetType());
                        StaffEntry entityInConflict = (StaffEntry)occ.Object;
                        Console.WriteLine("Table name: {0}", metatable.TableName);
                        Console.Write("Customer ID: ");
                        Console.WriteLine(entityInConflict.UZ + " " + entityInConflict.StuffPost + " " + entityInConflict.Branch + " " + entityInConflict.Quantity + " " + entityInConflict.Comment);
                        Console.ReadLine();
                    }
                }
                catch (DuplicateKeyException e)
                {
                    Console.WriteLine("DuplicateKeyException.");
                    Console.WriteLine(e.Message);
                    foreach (ObjectChangeConflict occ in mdb.ChangeConflicts)
                    {
                        MetaTable metatable = mdb.Mapping.GetTable(occ.Object.GetType());
                        StaffEntry entityInConflict = (StaffEntry)occ.Object;
                        Console.WriteLine("Table name: {0}", metatable.TableName);
                        Console.Write("Customer ID: ");
                        Console.WriteLine(entityInConflict.UZ + " " + entityInConflict.StuffPost + " " + entityInConflict.Branch + " " + entityInConflict.Quantity);
                        Console.ReadLine();
                    }
                }
               

            }
            mdb.SubmitChanges();
        }
    }
}
